package com.efounder.message.struct;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.ByteBuffer;


public class IMStruct003 extends IMStructAdpter
{
	private String msgId = null;
	private byte ackType = 0;
	private long time;


	public long getTime()
	{
		return time;
	}
	public void setTime(long time)
	{
		this.time = time;
	}
	public String getMsgId()
	{
		return msgId;
	}
	public void setMsgId(String msgId)
	{
		this.msgId = msgId;
	}



	public byte getAckType()
	{
		return ackType;
	}



	public void setAckType(byte ackType)
	{
		this.ackType = ackType;
	}



	public byte getMessageType()
	{
		return (byte)3;
	}


	public String toString()
	{
		return " ack msgId="+msgId;
	}
	public byte[] toByteArray()
	{
		int msgLenght = getMsgLength();
		byte[] byteArray = new byte[1+4+msgLenght];
		ByteBuffer buffer = ByteBuffer.wrap(byteArray);

		buffer.put(this.getMessageType());
		buffer.putInt(msgLenght);
		buffer.put(msgId.getBytes());
		buffer.put(ackType);
		buffer.putLong(time);

		return byteArray;

	}

	public int getMsgLength()
	{
		return 42+1+8;
	}

	public IMStruct copy()
	{
		IMStruct003 struct = new IMStruct003();
		struct.setFromClientId(this.getFromClientId());
		struct.setMsgId(this.getMsgId());
		struct.setAckType(this.getAckType());
		struct.setTime(this.getTime());
		return struct;
	}

	public static IMStruct getIMStruct(byte[] b)
	{
		ByteBuf buffer = Unpooled.buffer();
		buffer.writeBytes(b);
		return getIMStruct(buffer);
	}
	public static IMStruct getIMStruct(ByteBuf b)
	{
		IMStruct003 struct = new IMStruct003();
		struct.setMsgLength(b.readInt());

		byte[] msgIdByte = new byte[42];
		b.readBytes(msgIdByte);

		struct.setMsgId(new String(msgIdByte));
		struct.setAckType(b.readByte());
		struct.setTime(b.readLong());

		return struct;
	}
}
